<!-- 针车件资作业-2：蹇泽阳 -->
<template>
  <div class="app-container">
    <!-- 打印员工汇总表 -->
    <div class="printContent">
      <div v-for="(item, index) in printTag" :key="index" style="width: 100%" :style="{ height: '210mm' }">
        <div class="center size-16 title" style="padding-top: 20px">针车件资汇总表</div>
        <div class="header size-14">
          <span>操作员：{{ item.workName }}</span>
          <span>部门：{{ item.deptName }}</span>
          <span>生产日期{{ item.slipDate }}</span>
          <span>打印日期{{ item.printDate }}</span>
          <span>第{{ index + 1 }}页 (共 {{ item.total }}页)</span>
        </div>
        <div class="mainTable">
          <table style="text-align: center; margin-top: 5px">
            <tr v-for="(item2, rowIndex) in item.list" :key="rowIndex">
              <td class="size-12" v-for="(item3, colIndex) in item2" :key="colIndex" :style="{ textAlign: colIndex === 4 ? 'left' : 'center' }">
                {{ item3 }}
              </td>
            </tr>
          </table>
        </div>
        <div class="mainTable footer">
          <span style="margin-right: 100px">组长：</span>
          <span>操作员：</span>
        </div>
      </div>
    </div>

    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <Find :searchImg="false" :OriginalFilter="{ static: true }" :formList="formList2" @findClicked="findClicked2" />
    <div class="flex mt10" style="width: 100%; background-color: white">
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <h1 class="f16">指令产量明细</h1>
        <Table
          :tableHeader="tableHeader"
          :tableData="tableData"
          class="mt10"
          :isLoading="isLoading"
          @currentRow="currentRow"
          :summaryData="summaryData"
          summary
          @selectList="selectTableList"
        />
        <el-card shadow="never" :body-style="{ padding: '5px' }">
          <PageModule :PageData="{ total, buttonS: true }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
        </el-card>
      </el-card>
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import LoginWork from '@/utils/LoginWork'
import fileRequest from '@/utils/fileRequest'
import { nextTick } from 'vue'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'SewingPartsForWorkTwo'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import type { formListType } from '@/components/Find/index.vue'
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import service from '@/utils/request'
import { debounce } from 'lodash'
import { onMounted, watch } from 'vue'

const formList = ref([
  {
    type: 'select',
    selectValue: '',
    selectP: '类型',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'selectJ',
    selectValue: '',
    selectP: '工序',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '指令',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '序号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '型体',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '结算单号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '结算月份',
    inputValue: ''
  },
  {
    type: 'date',
    dateLabel: '派工日期',
    dateValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '组别',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'selectJ',
    selectValue: '',
    selectP: '员工',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'checkbox',
    checkboxLabel: '未结算',
    checked: true
  },
  {
    type: 'checkbox',
    checkboxLabel: '已结算',
    checked: false
  },
  {
    type: 'checkbox',
    checkboxLabel: '没单价',
    checked: false
  },
  {
    type: 'checkbox',
    checkboxLabel: '有单价',
    checked: true
  },
  {
    type: 'button',
    checkboxLabel: '查询'
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '导出'
  }
]) as any

const formList2 = ref([
  {
    type: 'dateMonth',
    dateLabel: '结算月份',
    dateValue: '',
    dateType: 'month'
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '结算方式',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'button',
    checkboxLabel: '结算确认'
  },
  {
    type: 'button',
    checkboxLabel: '取消结算'
  },
  {
    type: 'input',
    inputPlaceholder: '备注',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '财务备注'
  },

  {
    type: 'button',
    checkboxLabel: '导出员工汇总'
  },
  {
    type: 'button',
    checkboxLabel: '导出员工汇总表(XLS)'
  },
  {
    type: 'button',
    checkboxLabel: '导出员工汇总(WEB)'
  },
  {
    type: 'button',
    checkboxLabel: '导出月份汇总表'
  }
]) as any

const tableHeader = ref([
  {
    label: '类型',
    prop: 'type'
  },
  {
    width: 100,
    backgroundColor: true,
    label: '指令号',
    prop: 'lotNo'
  },
  {
    label: '型体',
    prop: 'style'
  },
  {
    label: '生产日期',
    prop: 'slipDate'
  },
  {
    label: '针车组别',
    prop: 'deptName'
  },
  {
    label: '工号',
    prop: 'workId'
  },
  {
    label: '姓名',
    prop: 'workName'
  },
  {
    label: '序号',
    prop: 'seqNo'
  },
  {
    width: '150px',
    label: '工序名称',
    prop: 'processName'
  },
  {
    label: '订单数量',
    prop: 'ordQty'
  },
  {
    label: '完工数量',
    prop: 'qty'
  },
  {
    label: '单价',
    prop: 'price'
  },
  {
    label: '金额',
    prop: 'amt'
  },
  {
    label: '确认结算',
    prop: 'checkFlag'
  },
  {
    label: '结算月份',
    prop: 'financeYymm'
  },
  {
    label: '结算单号',
    prop: 'balanceNo'
  },
  {
    label: '财务备注',
    prop: 'remark'
  },
  {
    label: '封账标识',
    prop: 'bonusFlag'
  }
])
// 表一模块
// 表一按钮
const findClicked = async (content: string) => {
  if (content === '查询') {
    getTableList()
  }
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  if (content === '导出') {
    const res = (await fileRequest.post(`/salaryStitchInventory/export`, {
      balanceNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '结算单号'
      )?.inputValue,
      checkFlag: cfmFlag(), //	结算状态(0:未结算,1:已结算,2:全部)	string	body	true
      dateEnd1: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '派工日期'
      )?.dateValue?.[1], // 派工日期
      dateStart1: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '派工日期'
      )?.dateValue?.[0],
      deptName: formList.value
        .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '组别')
        ?.selectValue.split('  ')[0],
      deptNo: formList.value
        .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '组别')
        ?.selectValue.split('  ')[1],
      financeYymm: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '结算月份'
      )?.inputValue,
      lotNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '指令'
      )?.inputValue,
      seqNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '序号'
      )?.inputValue,
      priceFlag: cfmFlag2(),
      processName: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '工序')
        ?.selectValue,
      style: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '型体'
      )?.inputValue,
      type: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '类型')
        ?.selectValue,
      workId: formList.value
        .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '员工')
        ?.selectValue.split('  ')[1],
      workName: formList.value
        .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '员工')
        ?.selectValue.split('  ')[0],
      pageNum: currentPage.value,
      pageSize: pageSize.value
    })) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`filename=`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
}
const findClicked2 = async (content: string) => {
  if (content === '结算确认') {
    priceDown(1)
  }
  if (content === '取消结算') {
    priceDown(2)
  }
  if (content === '财务备注') {
    priceDown(3, formList2.value[4].inputValue)
  }
  if (content === '导出员工汇总') {
    printSummary()
  }
  if (content === '导出员工汇总表(XLS)') {
    if (
      formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '工序')?.selectValue ||
      formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '指令'
      )?.inputValue ||
      formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '型体'
      )?.inputValue ||
      formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '结算单号'
      )?.inputValue ||
      formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '结算月份'
      )?.inputValue ||
      formList.value.find((item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '派工日期')
        ?.dateValue ||
      formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '组别')?.selectValue ||
      formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '员工')?.selectValue
    ) {
      const res = (await fileRequest.post(`/salaryStitchInventory/exportEmployeeSummary`, {
        balanceNo: formList.value.find(
          (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '结算单号'
        )?.inputValue,
        checkFlag: cfmFlag(), //	结算状态(0:未结算,1:已结算,2:全部)	string	body	true
        dateEnd1: formList.value.find(
          (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '派工日期'
        )?.dateValue?.[1], // 派工日期
        dateStart1: formList.value.find(
          (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '派工日期'
        )?.dateValue?.[0],
        deptName: formList.value
          .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '组别')
          ?.selectValue.split('  ')[0],
        deptNo: formList.value
          .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '组别')
          ?.selectValue.split('  ')[1],
        financeYymm: formList.value.find(
          (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '结算月份'
        )?.inputValue,
        lotNo: formList.value.find(
          (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '指令'
        )?.inputValue,
        priceFlag: cfmFlag2(),
        processName: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '工序')
          ?.selectValue,
        style: formList.value.find(
          (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '型体'
        )?.inputValue,
        type: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '类型')
          ?.selectValue,
        workId: formList.value
          .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '员工')
          ?.selectValue.split('  ')[1],
        workName: formList.value
          .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '员工')
          ?.selectValue.split('  ')[0],
        pageNum: currentPage.value,
        pageSize: pageSize.value
      })) as any
      const link = document.createElement('a') // 利用a标签自定义下载文件名
      let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
      link.style.display = 'none'
      //设置连接
      link.href = window.URL.createObjectURL(blob)
      const FileName = decodeURI(res.headers['content-disposition'].split(`filename=`)[1])
      link.download = FileName //自定义文件名字
      document.body.appendChild(link)
      //模拟点击事件
      link.click()
      document.body.removeChild(link)
    } else {
      JMessage.error('请选择条件')
    }
  }
  if (content === '导出月份汇总表') {
    if (formList2.value[0].dateValue === '') {
      JMessage.error('请选择结算月份和结算方式')
      return
    }
    const res = (await fileRequest.post(`/salaryStitchInventory/exportMonthSummary`, {
      balanceNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '结算单号'
      )?.inputValue,
      checkFlag: cfmFlag(), //	结算状态(0:未结算,1:已结算,2:全部)	string	body	true
      dateEnd1: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '派工日期'
      )?.dateValue?.[1], // 派工日期
      dateStart1: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '派工日期'
      )?.dateValue?.[0],
      deptName: formList.value
        .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '组别')
        ?.selectValue.split('  ')[0],
      deptNo: formList.value
        .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '组别')
        ?.selectValue.split('  ')[1],
      financeYymm: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '结算月份'
      )?.inputValue,
      lotNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '指令'
      )?.inputValue,
      seqNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '序号'
      )?.inputValue,
      priceFlag: cfmFlag2(),
      processName: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '工序')
        ?.selectValue,
      style: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '型体'
      )?.inputValue,
      type: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '类型')
        ?.selectValue,
      workId: formList.value
        .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '员工')
        ?.selectValue.split('  ')[1],
      workName: formList.value
        .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '员工')
        ?.selectValue.split('  ')[0],
      pageNum: currentPage.value,
      pageSize: pageSize.value
    })) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`filename=`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
  if (content === '导出员工汇总(WEB)') {
  }
}

const priceDown = async (type: number = 1, remark: string = '') => {
  ElMessageBox.confirm('确定吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    const res = (await LoginWork.post('/salaryStitchInventory/getSettleAccounts', {
      request: {
        balanceNo: formList.value.find(
          (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '结算单号'
        )?.inputValue,
        checkFlag: cfmFlag(), //	结算状态(0:未结算,1:已结算,2:全部)	string	body	true
        dateEnd1: formList.value.find(
          (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '派工日期'
        )?.dateValue?.[1], // 派工日期
        dateStart1: formList.value.find(
          (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '派工日期'
        )?.dateValue?.[0],
        deptName: formList.value
          .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '组别')
          ?.selectValue.split('  ')[0],
        deptNo: formList.value
          .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '组别')
          ?.selectValue.split('  ')[1],
        financeYymm: formList.value.find(
          (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '结算月份'
        )?.inputValue,
        lotNo: formList.value.find(
          (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '指令'
        )?.inputValue,
        seqNo: formList.value.find(
          (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '序号'
        )?.inputValue,
        priceFlag: cfmFlag2(),
        processName: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '工序')
          ?.selectValue,
        style: formList.value.find(
          (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '型体'
        )?.inputValue,
        type: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '类型')
          ?.selectValue,
        workId: formList.value
          .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '员工')
          ?.selectValue.split('  ')[1],
        workName: formList.value
          .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '员工')
          ?.selectValue.split('  ')[0],
        pageNum: currentPage.value,
        pageSize: pageSize.value
      },
      accountName: formList2.value[1].selectValue,
      financeYymm: formList2.value[0].dateValue,
      operateFlag: type,
      remark: remark
    })) as any
    if (res.code === 200) {
      JMessage.success(res.message)
      getTableList()
      formList2.value.forEach((item: any) => {
        item.inputValue = ''
        item.selectValue = ''
        item.dateValue = ''
      })
    } else {
      JMessage.error(res.message)
    }
  })
}
const tableData = ref([])
const imgList = ref([])
const isLoading = ref(true)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData = ref<summaryDataType[]>([
  {
    index: 10,
    num: 0
  },
  {
    index: 12,
    num: 0
  }
])
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
// 表格计算函数
const cfmFlag = () => {
  const Unaudited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '未结算')
  const Audited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '已结算')
  if (!Unaudited || !Audited) return
  if (Unaudited.checked && Audited.checked) {
    return '0'
  } else if (Unaudited.checked) {
    return '2'
  } else if (Audited.checked) {
    return '1'
  } else {
    ElMessage('必须至少选择一项')
    Unaudited.checked = true
    Audited.checked = true
    return '0'
  }
}
const cfmFlag2 = () => {
  const Unaudited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '没单价')
  const Audited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '有单价')
  if (!Unaudited || !Audited) return
  if (Unaudited.checked && Audited.checked) {
    return '0'
  } else if (Unaudited.checked) {
    return '1'
  } else if (Audited.checked) {
    return '2'
  } else {
    ElMessage('必须至少选择一项')
    Unaudited.checked = true
    Audited.checked = true
    return '0'
  }
}
// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>('/salaryStitchInventory/getLotProductDetails', {
    balanceNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '结算单号'
    )?.inputValue,
    checkFlag: cfmFlag(), //	结算状态(0:未结算,1:已结算,2:全部)	string	body	true
    dateEnd1: formList.value.find((item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '派工日期')
      ?.dateValue?.[1], // 派工日期
    dateStart1: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '派工日期'
    )?.dateValue?.[0],
    deptName: formList.value
      .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '组别')
      ?.selectValue.split('  ')[0],
    deptNo: formList.value
      .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '组别')
      ?.selectValue.split('  ')[1],
    financeYymm: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '结算月份'
    )?.inputValue,
    lotNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '指令'
    )?.inputValue,
    seqNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '序号'
    )?.inputValue,
    priceFlag: cfmFlag2(),
    processName: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '工序')
      ?.selectValue,
    style: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '型体'
    )?.inputValue,
    type: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '类型')
      ?.selectValue,
    workId: formList.value
      .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '员工')
      ?.selectValue.split('  ')[1],
    workName: formList.value
      .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '员工')
      ?.selectValue.split('  ')[0],
    pageNum: currentPage.value,
    pageSize: pageSize.value
  })
  isLoading.value = false
  tableData.value = tableDataList.list.map((item: any) => {
    if (item.checkFlag === 'Y') item.backgroundColor = '#80E080'
    return item
  })
  total.value = tableDataList.total
  currentRow()

  let sum = (await service.post('/salaryStitchInventory/getLotProductCount', {
    balanceNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '结算单号'
    )?.inputValue,
    checkFlag: cfmFlag(), //	结算状态(0:未结算,1:已结算,2:全部)	string	body	true
    dateEnd1: formList.value.find((item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '派工日期')
      ?.dateValue?.[1], // 派工日期
    dateStart1: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '派工日期'
    )?.dateValue?.[0],
    deptName: formList.value
      .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '组别')
      ?.selectValue.split('  ')[0],
    deptNo: formList.value
      .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '组别')
      ?.selectValue.split('  ')[1],
    financeYymm: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '结算月份'
    )?.inputValue,
    lotNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '指令'
    )?.inputValue,
    seqNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '序号'
    )?.inputValue,
    priceFlag: cfmFlag2(),
    processName: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '工序')
      ?.selectValue,
    style: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '型体'
    )?.inputValue,
    type: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '类型')
      ?.selectValue,
    workId: formList.value
      .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '员工')
      ?.selectValue.split('  ')[1],
    workName: formList.value
      .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '员工')
      ?.selectValue.split('  ')[0],
    pageNum: currentPage.value,
    pageSize: pageSize.value
  })) as any
  summaryData.value[0].num = sum.countQty
  summaryData.value[1].num = sum.countAmt
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
// 点击表给另一张表传数据
const currentTableRow = ref({}) as any
const currentRow = async (row: any = tableData.value[0] ? tableData.value[0] : {}) => {
  currentTableRow.value = row
}
const selectedLists = ref([])
const selectTableList = (selectedList: any) => {
  selectedLists.value = selectedList
}

const getSelectList = async () => {
  formList.value[0].selectOptions?.push(
    {
      optionKey: '针车正单',
      optionLabel: '针车正单',
      optionValue: '针车正单'
    },
    {
      optionKey: '针车补单',
      optionLabel: '针车补单',
      optionValue: '针车补单'
    }
  )
  const res = (await service.post('/back/getStitchpeProcess', {
    pageNum: '1',
    pageSize: '99999'
  })) as any
  res.list.forEach((item: any) => {
    formList.value[1].selectOptions?.push({
      value: item.processName,
      label: item.processName
    })
  })
  const res2 = (await service.post(`/back/getStitchpeDept`, {
    pageNum: '1',
    pageSize: '99999'
  })) as any
  res2.list.forEach((item: any) => {
    formList.value[8].selectOptions?.push({
      optionValue: `${item.collectName}  ${item.collectDept}`,
      optionLabel: item.collectName,
      optionKey: `${item.collectName}  ${item.collectDept}`
    })
  })
  const res3 = (await service.post('/back/getStitchEmployee', {
    pageNum: '1',
    pageSize: '99999'
  })) as any
  res3.list.forEach((item: any) => {
    formList.value[9].selectOptions?.push({
      value: `${item.name}  ${item.workerId}`,
      label: `${item.name}       ${item.workerId}`
    })
  })

  formList2.value[1].selectOptions?.push({
    optionKey: '荣阳大货',
    optionLabel: '荣阳大货',
    optionValue: '荣阳大货'
  })
}
onMounted(() => {
  getSelectList()
})

// 打印汇总表
const printTag = ref([]) as any
const printSummary = async () => {
  if (
    formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '工序')?.selectValue ||
    formList.value.find((item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '指令')
      ?.inputValue ||
    formList.value.find((item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '型体')
      ?.inputValue ||
    formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '结算单号'
    )?.inputValue ||
    formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '结算月份'
    )?.inputValue ||
    formList.value.find((item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '派工日期')
      ?.dateValue ||
    formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '组别')?.selectValue ||
    formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '员工')?.selectValue
  ) {
    const res = (await LoginWork.post(`/salaryStitchInventory/exportEmployeeSummaryByPdf`, {
      balanceNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '结算单号'
      )?.inputValue,
      checkFlag: cfmFlag(), //	结算状态(0:未结算,1:已结算,2:全部)	string	body	true
      dateEnd1: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '派工日期'
      )?.dateValue?.[1], // 派工日期
      dateStart1: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '派工日期'
      )?.dateValue?.[0],
      deptName: formList.value
        .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '组别')
        ?.selectValue.split('  ')[0],
      deptNo: formList.value
        .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '组别')
        ?.selectValue.split('  ')[1],
      financeYymm: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '结算月份'
      )?.inputValue,
      lotNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '指令'
      )?.inputValue,
      seqNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '序号'
      )?.inputValue,
      priceFlag: cfmFlag2(),
      processName: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '工序')
        ?.selectValue,
      style: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '型体'
      )?.inputValue,
      type: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '类型')
        ?.selectValue,
      workId: formList.value
        .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '员工')
        ?.selectValue.split('  ')[1],
      workName: formList.value
        .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '员工')
        ?.selectValue.split('  ')[0],
      pageNum: currentPage.value,
      pageSize: 30
    })) as any
    if (res.code === 200) {
      let printArr = [] as any
      res.data.list.forEach((item: any) => {
        item.datePageList.forEach((item2: any) => {
          printArr.push({
            list: item2,
            workName: item.workName,
            deptName: item.deptName,
            slipDate: item.slipDate,
            printDate: res.data.printDate,
            countQty: item.countQty,
            countAmt: item.countAmt,
            total: res.data.total
          })
        })
      })
      printTag.value = printArr
    } else {
      JMessage.error(res.message)
    }
  } else {
    JMessage.error('请输入起止派工时间,指令,型体，结算单号，结算月份，组别，员工')
  }
}
watch(printTag, () => {
  nextTick(() => {
    const qrCodeContainer = document.querySelector<any>('.printContent')
    if (qrCodeContainer) {
      qrCodeContainer.style.display = 'block'
      const printWindow = window.open('', '_blank')
      printWindow!.document.write(`
      <!DOCTYPE html>
      <html>
        <head>
          <title>打印标签</title>
          <link rel="stylesheet" type="text/css" href="print.css" media="print">
          <style>
            body, html {
              margin: 0;
              padding: 0;
            }
            @page {
              size: auto;
              margin: 0;
            }
            @media print {
                /* 打印时的样式 */

                /* 其他打印样式 */
            }
            h2 {
                    margin-left: initial; /* 或者设置适合打印的左边距 */
                    page-break-inside: avoid; /* 避免在h2元素内部分页 */
            }
            .pad3 {
                padding: 3px;
            }

            .main {
                display: flex;
                justify-content: center;
            }

            .left {
                display: flex;
                flex-direction: column;
                justify-content: center;
                align-items: center;
                margin-right: 5px;
            }


            .title {
                display: flex;
                justify-content: center;
                margin-bottom: 5px;
            }

            .center {
                text-align: center;
            }

            .right {
                text-align: right;
            }

            .left {
                text-align: left;
            }

            .no-padding {
                padding: 0;
            }

            .no-margin {
                margin: 0;
            }

            .margin-5 {
                margin: 5px;
            }

            .margin-top-5 {
                margin-top: 5px;
            }

            .margin-top-15 {
                margin-top: 15px;
            }

            .size-8 {
                font-size: 8px !important;
            }

            .size-10 {
                font-size: 10px;
            }

            .size-12 {
                font-size: 12px;
            }

            .size-14 {
                font-size: 14px;
            }

            .size-16 {
                font-size: 17px;
            }

            .size-18 {
                font-size: 18px;
            }

            .size-24 {
                font-size: 24px;
            }

            #date-tip {
                position: relative;
            }

            span.tip {
                display: inline-block;
                height: 14px;
                line-height: 14px;
                margin-right: 30px;
            }

            table {
                font-size: 10px;
                border-collapse: collapse;
                border-spacing: 0;
                border-collapse: collapse;
                border-spacing: 0;
                empty-cells: show;
                width: 100%;
            }

            table th,
            table td {
                border: 1px solid #000;
                font-size: 12px;
            }

            table.no-border,
            table.no-border th,
            table.no-border td {
                border: 0px;
            }

            table td.no-left-border {
                border-left: 0px;
            }

            table td.no-right-border {
                border-right: 0px;
            }

            .mainTable {
                margin: 0px 30px;
            }

            /* 备料派工单汇总表 */
            .header {
                display: flex;
                justify-content: space-evenly;
            }

            .footer {
                margin-top: 20px;
                margin-right: 100px;
                display: flex;
                justify-content: right;
            }



            /* 可以在这里添加更多自定义的打印样式 */
          </style>
        </head>
        <body>
    `)

      // 将 .qrCodeContainer 的内容复制到打印窗口的 body 中
      const cloneContainer = qrCodeContainer.cloneNode(true)
      printWindow!.document.body.appendChild(cloneContainer)

      printWindow!.document.write(`
        </body>
      </html>
    `)
      printWindow!.document.close()

      // 开始打印
      printWindow!.print()

      // 打印完成后，可以恢复 .qrCodeContainer 的原始显示状态
      qrCodeContainer.style.display = 'none'
    }
  })
})

// Helper function to paginate the dateList
function paginate(array: any[], pageSize: number) {
  return array.reduce((acc, curr, index) => {
    const pageIndex = Math.floor(index / pageSize)
    if (!acc[pageIndex]) {
      acc[pageIndex] = []
    }
    acc[pageIndex].push(curr)
    return acc
  }, [] as any[][])
}
</script>

<style lang="less" scoped>
table {
  table-layout: auto !important;
}

:deep(.el-table__header) {
  width: 100% !important;
}

:deep(.el-table__body) {
  width: 100% !important;
}
</style>
